package org.sdnstproject.interlayer.interlayerProxy;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Configuration {
	private static final Logger log = LoggerFactory.getLogger(Configuration.class);
	private String controllerIP;
	private String controllerUser;
	private String switchIP;
	private String switchUser;
	private String localIP;
	private String interlayerPort;
	private String controllerPort;
	private String targetController;
	private String OpenFlowVersion;
	private String managerPort;
	private String Interlayer_ROOT;
	//非人工配置项
	private String controllerSSH;
	private String switchSSH;
	private byte ofVersion;
	private ArrayList<String> switchList = new ArrayList<String>();
	
	private static Configuration instance = new Configuration();
    public Configuration() {
    	readConfigFile();
    	checkConfig();
    	this.controllerSSH = controllerUser+"@"+controllerIP;
    	this.switchSSH = switchUser+"@"+switchIP;
    	if (OpenFlowVersion.equals("1.0"))
    		this.ofVersion = (byte) 1;
        else if (OpenFlowVersion.equals("1.3"))
        	this.ofVersion = (byte) 4;
    }
    private void checkConfig() {
		if (controllerIP == null) log.info("controllerIP == null");
		if (controllerUser == null) log.info("controllerUser == null");
		if (switchIP == null) log.info("switchIP == null");
		if (switchUser == null) log.info("switchUser == null");
		if (localIP == null) log.info("localIP == null");
		if (interlayerPort == null) log.info("interlayerPort == null");
		if (controllerPort == null) log.info("controllerPort == null");
		if (targetController == null) log.info("targetController == null");
		if (OpenFlowVersion == null) log.info("OpenFlowVersion == null");
		if (managerPort == null) log.info("managerPort == null");
		if (Interlayer_ROOT == null) log.info("Interlayer_ROOT == null");
	}
	public static Configuration getInstance() {
		if(instance == null) {
			System.exit(-1);
		}
        return instance;
    }
	public void setInstance(Configuration config) {
		instance = config;
	}
    
    public void readConfigFile() {
        BufferedReader br = null;
        String temp = "";
        try {
//        	InputStream in = Configuration.class.getClassLoader().getResourceAsStream("./src/main/java/config.properties");
//            br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
//        	File file = new File("/home/ubuntu/eclipse-workspace/Interlayer/config.properties");
        	File file = new File("./config.properties");
//        	File file = new File(path);
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));

            while ((temp = br.readLine()) != null) {
            	if(temp.contains("#")) continue;
                if (temp.contains("controllerIP")) {
                	this.controllerIP = temp.substring(temp.indexOf("=") + 1);
                } else if (temp.contains("controllerUser")) {
                	this.controllerUser = temp.substring(temp.indexOf("=") + 1);
				} else if (temp.contains("switchIP")) {
                	this.switchIP = temp.substring(temp.indexOf("=") + 1);
				} else if (temp.contains("switchUser")) {
                	this.switchUser = temp.substring(temp.indexOf("=") + 1);
				} else if (temp.contains("localIP")) {
                	this.localIP = temp.substring(temp.indexOf("=") + 1);
				} else if (temp.contains("interlayerPort")) {
                	this.interlayerPort = temp.substring(temp.indexOf("=") + 1);
				} else if (temp.contains("controllerPort")) {
                	this.controllerPort = temp.substring(temp.indexOf("=") + 1);
				} else if (temp.contains("targetController")) {
                	this.targetController = temp.substring(temp.indexOf("=") + 1);
				}  else if (temp.contains("OpenFlowVersion")) {
                	this.OpenFlowVersion = temp.substring(temp.indexOf("=") + 1);
				}  else if (temp.contains("managerPort")) {
                	this.managerPort = temp.substring(temp.indexOf("=") + 1);
				}  else if (temp.contains("Interlayer_ROOT")) {
                	this.Interlayer_ROOT = temp.substring(temp.indexOf("=") + 1);
				}
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.exit(-1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
            	if(br != null) {
            		br.close();
            	}
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
	public String getControllerIP() {
		return controllerIP;
	}
	public void setControllerIP(String controllerIP) {
		this.controllerIP = controllerIP;
	}
	public String getControllerUser() {
		return controllerUser;
	}
	public void setControllerUser(String controllerUser) {
		this.controllerUser = controllerUser;
	}
	public String getSwitchIP() {
		return switchIP;
	}
	public void setSwitchIP(String switchIP) {
		this.switchIP = switchIP;
	}
	public String getSwitchUser() {
		return switchUser;
	}
	public void setSwitchUser(String switchUser) {
		this.switchUser = switchUser;
	}
	public String getLocalIP() {
		return localIP;
	}
	public void setLocalIP(String localIP) {
		this.localIP = localIP;
	}
	public String getTargetController() {
		return targetController;
	}
	public void setTargetController(String targetController) {
		this.targetController = targetController;
	}
	public String getOpenFlowVersion() {
		return OpenFlowVersion;
	}
	public void setOpenFlowVersion(String openFlowVersion) {
		OpenFlowVersion = openFlowVersion;
	}
	public String getManagerPort() {
		return managerPort;
	}
	public void setManagerPort(String managerPort) {
		this.managerPort = managerPort;
	}

	public String getInterlayer_ROOT() {
		return Interlayer_ROOT;
	}
	public void setInterlayer_ROOT(String interlayer_ROOT) {
		Interlayer_ROOT = interlayer_ROOT;
	}
	public String getControllerSSH() {
		return controllerSSH;
	}
	public void setControllerSSH(String controllerSSH) {
		this.controllerSSH = controllerSSH;
	}
	public String getSwitchSSH() {
		return switchSSH;
	}
	public void setSwitchSSH(String switchSSH) {
		this.switchSSH = switchSSH;
	}
	public void setInterlayerPort(String interlayerPort) {
		this.interlayerPort = interlayerPort;
	}
	public void setControllerPort(String controllerPort) {
		this.controllerPort = controllerPort;
	}
	
	public String getInterlayerPort() {
		return interlayerPort;
	}
	public String getControllerPort() {
		return controllerPort;
	}
	public byte getOfVersion() {
		return ofVersion;
	}
	public void setOfVersion(byte ofVersion) {
		this.ofVersion = ofVersion;
	}
	public ArrayList<String> getSwitchList() {
        return switchList;
    }
	public void setSwitchList(ArrayList<String> switchList) {
        this.switchList = switchList;
    }
	@Override
	public String toString() {
		return "#Configuration:"
				+"\ncontrollerIP=" + controllerIP
				+"\ncontrollerUser="+controllerUser
				+"\nswitchIP="+switchIP
				+"\nswitchUser="+switchUser
				+"\nlocalIP="+localIP
				+"\ninterlayerPort="+interlayerPort
				+"\ncontrollerPort="+controllerPort
				+"\ntargetController="+targetController
				+"\nOpenFlowVersion="+OpenFlowVersion
				+"\nmanagerPort="+managerPort
				+"\nInterlayer_ROOT="+Interlayer_ROOT;
	}
}